package eval.gui;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.List;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.DeviationRenderer;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import eval.utilities.configuration.ExperimentConfiguration;

/**
 * create the panel for the GUI
 * */
class PlotPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	public final static boolean writeToImgFile = 
			ExperimentConfiguration.writeToImgFile;

	private String xLabel = null;
	private String yLabel = null;

	private boolean showLegend = false;

	private List<XYIntervalSeries> seriesList;

	/**
	 * @param xLabel
	 * @param yLabel
	 * @param showLegend
	 * @param seriesList
	 * @param type
	 * @param chartLabels
	 * @param resFileName
	 */
	PlotPanel(String xLabel, String yLabel, boolean showLegend,
			List<XYIntervalSeries> seriesList, int type,String chartLabels,String resFileName) {
		this.xLabel = xLabel;
		this.yLabel = yLabel;
		this.showLegend = showLegend;
		this.seriesList = seriesList;

		init(type,chartLabels,resFileName);
	}

	private void init(int type,String chartLabels,String resFileName) {
		setLayout(new BorderLayout());
		if (type == 0)
			add(createChartPanel(seriesList,chartLabels,resFileName));
		else
			return;
	}

	private ChartPanel createChartPanel(List<XYIntervalSeries> seriesList,String chartLabels,String resFileName) {

		final XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();

		for (XYIntervalSeries series : seriesList) {
			dataset.addSeries(series);
		}

		final JFreeChart chart = ChartFactory.createXYLineChart("", "", "",
				dataset, PlotOrientation.VERTICAL, showLegend, false, false);

		final XYPlot plot = chart.getXYPlot();

		final DeviationRenderer renderer = new DeviationRenderer(true, false);

		renderer.setAlpha(0.2f);
		renderer.setSeriesFillPaint(0, new Color(0, 0, 205));
		renderer.setSeriesPaint(0, new Color(0, 0, 205));
		renderer.setSeriesStroke(0, new BasicStroke(1.2f));

		renderer.setSeriesPaint(1, new Color(192, 0, 0));
		renderer.setSeriesStroke(1,
				new BasicStroke(1.2f, BasicStroke.CAP_BUTT,
						BasicStroke.JOIN_MITER, 10.0f,
						new float[] { 5.0f, 2.0f }, 0.0f));
		renderer.setSeriesFillPaint(1, new Color(255, 218, 185));
		renderer.setSeriesFillPaint(1, new Color(192, 0, 0));

		renderer.setSeriesFillPaint(2, new Color(193, 255, 193));
		renderer.setSeriesFillPaint(2, new Color(0, 200, 0));
		renderer.setSeriesPaint(2, new Color(0, 200, 0));
		renderer.setSeriesStroke(2,
				new BasicStroke(1.2f, BasicStroke.CAP_BUTT,
						BasicStroke.JOIN_MITER, 10.0f,
						new float[] { 1.5f, 1.5f }, 0.0f));

		renderer.setSeriesFillPaint(3, new Color(234, 234, 174));
		renderer.setSeriesFillPaint(3, new Color(255, 127, 0));
		renderer.setSeriesPaint(3, new Color(255, 127, 0));
		renderer.setSeriesStroke(3, new BasicStroke(1.2f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_MITER, 10.0f, new float[] { 5.0f, 1.5f, 2.0f,
				1.5f }, 0.0f));

		renderer.setSeriesFillPaint(4, new Color(200, 200, 200));
		renderer.setSeriesFillPaint(4, new Color(0, 0, 0));
		renderer.setSeriesPaint(4, new Color(0, 0, 0));
		renderer.setSeriesStroke(4,
				new BasicStroke(1.2f, BasicStroke.CAP_BUTT,
						BasicStroke.JOIN_MITER, 10.0f,
						new float[] { 2.0f, 4.0f }, 0.0f));
		plot.setRenderer(renderer);
		final NumberAxis domainAxis = new NumberAxis(xLabel);
		domainAxis.setTickLabelFont(new Font(Font.SANS_SERIF,3,14));
		final NumberAxis rangeAxis = new NumberAxis(yLabel);
		rangeAxis.setTickLabelFont(new Font(Font.SANS_SERIF,3,14));
		plot.setDomainAxis(domainAxis);
		plot.setRangeAxis(rangeAxis);
		plot.setBackgroundPaint(new Color(255, 255, 255));
		plot.setDomainGridlinePaint(new Color(200, 200, 200));
		plot.setRangeGridlinePaint(new Color(200, 200, 200));

		ChartPanel chartPanel = new ChartPanel(chart);
		if(writeToImgFile)
			PlotIOSerializer.writeFile(chart,chartLabels,resFileName);
		return chartPanel;
	}

}
